package com.liuzhongni.wahahanews.activity;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Telephony;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.liuzhongni.wahahanews.R;
import com.liuzhongni.wahahanews.adapter.ViewPagerAdapterOfNewPhotoDatails;
import com.liuzhongni.wahahanews.headlineclass.NewHeadlineDatails;
import com.liuzhongni.wahahanews.headlineclass.ResponseEtyThird;
import com.liuzhongni.wahahanews.singleton.StringRequestUtf;
import com.liuzhongni.wahahanews.singleton.VolleySingleton;

import java.util.ArrayList;


/**
 * Created by admin on 2016/1/14.
 */
public class NewsHeadlineDetailsActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
    private ViewPager viewPager;
    private ViewPagerAdapterOfNewPhotoDatails adapter;
    private NewHeadlineDatails datails= new NewHeadlineDatails();
    private TextView textViewtitle;
    private TextView textViewpagers;
    private TextView textViewdigest;
    private RelativeLayout rl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newheadline_details);

        photoDatailsJson();

    }

    private void photoDatailsJson() {
        Intent intent = getIntent();
        String url ="http://c.m.163.com/photo/api/set/"+intent.getStringExtra("url")+".json";
        Log.d("NewsHeadlineDetailsActi", url);
        StringRequestUtf request = new StringRequestUtf(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("NewsHeadlineDetailsActi", response);
                Gson gson = new Gson();
                datails = gson.fromJson(response, NewHeadlineDatails.class);
                init();

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("NewsHeadlineDetailsActi", "失败");
            }
        });
        VolleySingleton.getOurInstance().addRequest(request);
    }

    private void init() {
        viewPager = (ViewPager) findViewById(R.id.newheadline_details_vp);
        textViewtitle = (TextView) findViewById(R.id.newheadline_details_title);
        textViewpagers = (TextView) findViewById(R.id.newheadline_details_pagers);
        textViewdigest = (TextView) findViewById(R.id.newheadline_details_digest);
        rl = (RelativeLayout)findViewById(R.id.newheadline_details_rl);
        adapter = new ViewPagerAdapterOfNewPhotoDatails(NewsHeadlineDetailsActivity.this, datails.getPhotos(),rl);
        viewPager.setAdapter(adapter);
        viewPager.setOnPageChangeListener(this);
        textViewtitle.setText(datails.getSetname());
        textViewdigest.setText(adapter.getDatailses().get(0).getNote());
        textViewpagers.setText((0+1)+"/"+adapter.getDatailses().size());

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        textViewtitle.setText(datails.getSetname());
        textViewdigest.setText(adapter.getDatailses().get(position).getNote());
        textViewpagers.setText((position+1)+"/"+adapter.getDatailses().size());

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("NewsHeadlineDetailsActi", "stop");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("NewsHeadlineDetailsActi", "pause");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("NewsHeadlineDetailsActi", "resume");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("NewsHeadlineDetailsActi", "destroy");
    }
}
